package net.sunofbeach.blog.controller.portal;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sunofbeach.blog.pojo.Category;
import net.sunofbeach.blog.pojo.FriendLink;
import net.sunofbeach.blog.pojo.Looper;
import net.sunofbeach.blog.pojo.User;
import net.sunofbeach.blog.response.Result;
import net.sunofbeach.blog.service.*;
import net.sunofbeach.blog.utils.Constants;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端获取网站信息控制器
 * </p>
 *
 * @author Bing
 * @since 2021-11-24
 */
@Api(tags = "门户网站信息")
@RestController
@RequestMapping("/portal/web_size_info")
public class WebSizeInfoPortalApi {

    @Resource
    private CategoryService categoryService;

    @Resource
    private FriendsService friendsService;

    @Resource
    private UserService userService;

    @Resource
    private LooperService looperService;

    @Resource
    private SettingService settingService;


    @ApiOperation("获取分类列表")
    @GetMapping("/category")
    public Result getCategory() {
        // 只能获取正常状态的分类
        List<Category> list = categoryService.list(new LambdaQueryWrapper<Category>().eq(Category::getStatus, Constants.Status.NORMAL).orderByDesc(Category::getCreateTime));
        return Result.ok(list);

    }

    @ApiOperation("获取网站标题")
    @GetMapping("/title")
    public Result getWebSizeTitle() {
        return settingService.getWebSizeTitle();
    }


    @ApiOperation("网站文章浏览总数")
    @GetMapping("/view_count")
    public Result getWebSizeInfoViewCount() {
        return settingService.getSizeViewCount();
    }

    @ApiOperation("获取网站SEO信息")
    @GetMapping("/seo")
    public Result getWebSizeSeoInfo() {
        return settingService.getSeoInfo();
    }


    @ApiOperation("轮播图列表")
    @GetMapping("/loop")
    public Result getLoop() {
        // 判断角色
        User user = userService.checkSobUser();
        // 普通用户、未登录用户，只能获取正常的 category
        if (user == null || !Constants.User.ROLE_ADMIN.equals(user.getRole())) {
            // 只能获取正常状态的分类
            List<Looper> list = looperService.list(new LambdaQueryWrapper<Looper>().eq(Looper::getState, Constants.Status.NORMAL).orderByDesc(Looper::getCreateTime));
            return Result.ok(list);

        } else {
            // 管理员，获取所有分类信息
            List<Looper> list = looperService.list(new LambdaQueryWrapper<Looper>().orderByDesc(Looper::getCreateTime));
            return Result.ok(list);
        }
    }


    /**
     * 列表
     *
     * @return
     */
    @ApiOperation("友情链接列表")
    @GetMapping("/friendList")
    public Result listFriendLinks() {
        // 判断角色
        User user = userService.checkSobUser();
        // 普通用户、未登录用户，只能获取正常的 category
        if (user == null || !Constants.User.ROLE_ADMIN.equals(user.getRole())) {
            // 只能获取正常状态的分类
            List<FriendLink> list = friendsService.list(new LambdaQueryWrapper<FriendLink>().eq(FriendLink::getState, Constants.Status.NORMAL).orderByDesc(FriendLink::getState).orderByAsc(FriendLink::getSort).orderByDesc(FriendLink::getCreateTime));
            return Result.ok(list);
        } else {
            // 管理员，获取所有分类信息
            List<FriendLink> list = friendsService.list(new LambdaQueryWrapper<FriendLink>().orderByDesc(FriendLink::getState).orderByAsc(FriendLink::getSort).orderByDesc(FriendLink::getCreateTime));
            return Result.ok(list);
        }
    }

}

